import Taro, { Component } from '@tarojs/taro'
import { Swiper, SwiperItem, Image, View, Text,Button  } from '@tarojs/components'
import '../../assets/common.scss'
import CommonJs from '../../utils/common'
import Footer from '../index/footer'

import cat01 from '../../assets/kedouimg01/cat08.png'
import cat02 from '../../assets/kedouimg01/tongshi.png'
import cat03 from '../../assets/kedouimg01/cwsj.png'
import cat04 from '../../assets/kedouimg01/dyhfw.png'


export default class Index extends Component {
  constructor(props) {
    super(props);
    this.state = {
    	tgmonery:0,
    	result:[],
     znwns:'',
     pageNumber:0,
     hasNextPage:false,
     scrollPd:false,
     pageheight:0,
    	tglist:[],
    	numinfo:'',
    	totalRows:0,
    	typeId:0,//类型id : 1-易客 2-易商 3-易栈 4-易桩 0-无,
    	mysj:'',
    	userinfo:'',  
	appmark:CommonJs.getmark()
    }
  }
  config = {
    navigationBarTitleText:'加载中'
  }

  componentWillMount () {
	if(this.state.appmark=='xjzb'){
		Taro.setNavigationBarTitle({
			title:'共享链'
		  })
		var color='#043581'
	}
	else{
		var color='#f7594d'
		Taro.setNavigationBarTitle({
			title:'关系'
		  })
	}
  	Taro.setNavigationBarColor({
		    frontColor: '#ffffff',
		    backgroundColor:color,
		    animation: {
		        duration: 400,
		        timingFunc: 'easeIn'
		    }
		})
  	var _this = this
    Taro.getSystemInfo({
      success:function(res){
        _this.setState({
          pageheight: res.windowHeight
        })
        _this.gettglist(0)
      }
    })
    var dateinfo = Taro.getStorageSync('dateinfo')
    var userinfo=Taro.getStorageSync('userinfo');
    userinfo.userid=dateinfo.userid;
    this.setState({
      userinfo:userinfo
    })
    this.getnum();
    this.getmysj();
    this.gettg();
    wx.showShareMenu({
	    withShareTicket: true,
	    menus: ['shareAppMessage']
	  });
  }
  componentDidShow () {
		
  }
  componentDidUpdate () {

  }
  getmysj(){
  	
  	var url='/retail/papi/v1.9/sharer/getusers/introducer'
  	var _this=this
  	CommonJs.ajaxRequest(url,'','GET',function(res){
  		var ret=res.data;
  		var arr=[{},{}];
  		if(ret.gradeChainList.length>0){
  			var red=ret.gradeChainList;
  			for(var i=0;i<red.length;i++){
  				if(red[i].gradeSettingId==2){
  					arr[0]=red[i]
  				}
  				if(red[i].gradeSettingId==3){
  					arr[1]=red[i]
  				}
  			}
  		}
  		ret.sjlist=arr;
		_this.setState({
	      mysj:ret
	    })
  	},"wfw")
  }
	gettg(){
  	var url='/retail/papi/v1.9/sharer/user/amount'
  	var _this=this
  	CommonJs.ajaxRequest(url,'','GET',function(res){
		_this.setState({
	      tgmonery:res.data
	    })
  	},"wfw")
  }
	getnum(){
  	var url='/retail/papi/v1.9/sharer/promotionNumber/'+Taro.getStorageSync('shopId')
  	var _this=this
  	CommonJs.ajaxRequest(url,'','GET',function(res){
		_this.setState({
	      numinfo:res.data
	    })
  	},"wfw")
  }
	istg(){
  	var url='/retail/papi/v1.9/sharer/customer/sharing'
  	var _this=this
  	CommonJs.ajaxRequest(url,'','GET',function(res){
		if(res.data==null||res.data.length==0){
			Taro.showModal({
			  title: '开通会员，享受推广商权益',
			  content: '成为本店的推广大使，享受推广佣金，实时到账',
			  success: function (res) {
			    if (res.confirm) {
			     // CommonJs.openTo('/pages/tuiguang/enter');
			      CommonJs.openTo('/pages/card/cardadd');
			    } 
			    else if (res.cancel) { 
			    	Taro.showModal({
					  title: '是否查看推广订单',
					  success: function (res) {
					  	if (res.confirm) {
					  		CommonJs.openTo('/pages/tuiguang/index?type='+res.data[0].id);
					    } 
					  }
					})
			    }
			  }
			})
		}
		else{
			
			CommonJs.openTo('/pages/tuiguang/index?type='+res.data[0].id);
		}
  	},"wfw")
  	
  }
	loadMore(){
    var _this = this
    var pd = _this.state.scrollPd
    if(pd) {
      //防止多次触发滑动到底部事件
      _this.setState({
        scrollPd: false//禁止下拉
      })
      var pageNumber = _this.state.pageNumber + 1
      _this.gettglist(pageNumber)
    }else{
      var hasNextPage =  _this.state.hasNextPage
      if(hasNextPage){
        Taro.showToast({
          title:'正在加载数据...',
          icon: 'none',
          duration: 2000
        })
      }else{
        Taro.showToast({
          title:'没有更多数据啦！',
          icon: 'none',
          duration: 2000
        })
      }
    }
  }
	gettglist(pageNumber){
		
		var url='/retail/papi/v1.9/sharer/query/subordinates'
		var _this=this
		var dates={
			shopId:Taro.getStorageSync('shopId'),
  		size:10,
  		page:pageNumber
  	}
		var typeId=this.state.typeId;
		if(typeId){
			dates.typeId=typeId
		}
  	CommonJs.ajaxRequest(url,dates,'GET',function(res){
			if (res) {

  			if(res.data.totalPages>pageNumber){
  		  	var hasNextPage = true;
  		  }
  			else{
  				var hasNextPage = false;
  			}
  			var result = res.data.list
  			if(pageNumber==0){
  				  var results = []
  			}
  			else{
  				  var results = _this.state.result
  			}
	      for(var i = 0; i < result.length; i++){
	        results.push(result[i]);
	      }
	      if (hasNextPage) {
	        _this.setState({
	          result: results,
	          znwns: '暂无数据',
	          pageNumber: res.data.page,
	          hasNextPage: hasNextPage,
	          totalRows:res.data.totalRows,
	          scrollPd: true
	        })
	      }
	      else {
	        _this.setState({
	          result: results,
	          znwns: '暂无数据',
	          pageNumber: res.data.page,
	          hasNextPage: hasNextPage,
	          totalRows:res.data.totalRows,
	          scrollPd: false
	        })
	      }
	    } else {
	      _this.setState({
	        znwns: '暂无数据'
	      })
	    }
  	},"wfw")
	}
	choose(val){
		this.setState({
	    typeId:val,
	    result:[]
	  })
		this.gettit();
		var _this=this;
		setTimeout(function(){
			_this.gettglist(0)
		},600)
	}
	gettit(){
		var typeId=this.state.typeId;
		if(typeId==0){
			return '用户'
		}
		else if(typeId==1){
			return '易客'
		}
		else if(typeId==2){
			return '易商'
		}
		else if(typeId==3){
			return '易栈'
		}
		else if(typeId==4){
			return '易庄'
		}
		else{
			return ''
		}
	}
  render () {
  	 const result = this.state.result
  	 const numinfo=this.state.numinfo
  	 const userinfo=this.state.userinfo
  	 const mysj=this.state.mysj
	 const appmark=this.state.appmark
    return (
    	<View className='wgmpage'>
    		
    		<View className='gxl-bg' style={appmark=='xjzb'?'':'display:none'}><Image className='img' mode='widthFix' src='https://xjzbsmall.g2ma.com//kedouimg/gxqbg.019703d5.png'></Image></View>
    		<View className={appmark=='xjzb'?'gxl-box gxl-boxts':'gxl-box'}>
    		<ScrollView className='qun-goods' scrollY='true' style={'height:'+this.state.pageheight+'px'} onScrollToLower={this.loadMore.bind(this,'')}>
    		<View className='gxq-tj'>
    				<View className='gxq-text'>总推广收益</View>
    				<View className='gxq-num' onClick={this.istg.bind(this)}>{this.state.tgmonery/100}</View>
    				
    				<View className='gxq-mytj'>
    					<View className='gxq-mytj-li'>
    							<Text className='gxq-mytj-text'>我的关系</Text>
    							<Text className='gxq-mytj-num'>{this.state.totalRows}</Text>
    					</View>
    					<View className='gxq-mytj-li'>
    							<Text className='gxq-mytj-text' style={appmark=='xjzb'?'':'display:none'}>我的易栈</Text>
    							<Text className='gxq-mytj-text' style={appmark=='xjzb'?'display:none':''}>街道合伙人</Text>
    							<Text className='gxq-mytj-num'>{mysj.sjlist[0].nickName?mysj.sjlist[0].nickName:'暂无'}</Text>
    					</View>
    					<View className='gxq-mytj-li'>
    							<Text className='gxq-mytj-text' style={appmark=='xjzb'?'':'display:none'}>我的易庄</Text>
    							<Text className='gxq-mytj-text' style={appmark=='xjzb'?'display:none':''}>区县合伙人</Text>
    							<Text className='gxq-mytj-num' >{mysj.sjlist[1].nickName?mysj.sjlist[1].nickName:'暂无'}</Text>
    					</View>
    				</View>
    			</View>
    		
    		<View className='typedl-box typedl-boxts'>
    			<View className='typedl' onClick={this.choose.bind(this,1)}>
    				<View className='typedt'><Image className="img" mode="widthFix" src={cat01}></Image></View>
    				<View className='typedd' style={appmark=='xjzb'?'':'display:none'}>易客({numinfo.customerNum})</View>
    				<View className='typedd' style={appmark=='xjzb'?'display:none':''}>用户({numinfo.customerNum})</View>
    			</View>
    			<View className='typedl' onClick={this.choose.bind(this,2)}>
    				<View className='typedt'><Image className="img" mode="widthFix" src={cat02}></Image></View>
    				<View className='typedd' style={appmark=='xjzb'?'':'display:none'}>易商({numinfo.memberNum})</View>
    				<View className='typedd' style={appmark=='xjzb'?'display:none':''}>合伙人({numinfo.memberNum})</View>
    			</View>
    			<View className='typedl' onClick={this.choose.bind(this,3)}>
    				<View className='typedt'><Image className="img" mode="widthFix" src={cat03}></Image></View>
    				<View className='typedd' style={appmark=='xjzb'?'':'display:none'}>易栈({numinfo.easyStacksNum})</View>
    				<View className='typedd' style={appmark=='xjzb'?'display:none':''}>街道合伙人({numinfo.easyStacksNum})</View>
    			</View>
    			<View className='typedl' onClick={this.choose.bind(this,4)}>
    				<View className='typedt'><Image className="img" mode="widthFix" src={cat04}></Image></View>
    				<View className='typedd' style={appmark=='xjzb'?'':'display:none'}>易庄({numinfo.easyPileNum})</View>
    				<View className='typedd' style={appmark=='xjzb'?'display:none':''}>区县合伙人({numinfo.easyPileNum})</View>
    			</View>
    		</View>
    		
    		<View className='typedl-box typedl-boxts01'>
    			<View className='qxl-tit'>我推广的{this.gettit()}</View>
    			<View className='typedl'  onClick={this.opendetails1.bind(this,'/pages/persinfo/persinfo',mysj.id)}>
    				<View className='typedt'><Image className='img' mode='widthFix' src={mysj.imageUrl?mysj.imageUrl:CommonJs.getlogo()}></Image></View>
    				<View className='typedd'>
    					<View className='typeddh3'>{mysj.nickName}(我的推荐人)</View>
    					<View className='typeddspan'>{mysj.mobile?mysj.mobile:'暂无'}</View>
    					
    				</View>
    			</View>
    			<View className='typedl typedlmy'  onClick={this.opendetails1.bind(this,'/pages/persinfo/persinfo',userinfo.userid)}>
    				<View className='typedt'><Image className='img' mode='widthFix' src={userinfo.avatarUrl?userinfo.avatarUrl:CommonJs.getlogo()}></Image></View>
    				<View className='typedd'>
    					<View className='typeddh3'>{userinfo.nickName}</View>
    					<View className='typeddspan'>{userinfo.mobile?userinfo.mobile:'暂无'}</View>
    					<View className='typeline'></View>
    				</View>
    			</View>
    			<View  style={result&&result.length>0?'padding-bottom:120rpx':'display:none'}>
		        	{result.map((item, index) => (
    			<View className='typedl'  onClick={this.opendetails1.bind(this,'/pages/persinfo/persinfo',item.shareUserId)}>
    				<View className='typedt'><Image className='img' mode='widthFix' src={item.avatar?item.avatar:CommonJs.getlogo()}></Image></View>
    				<View className='typedd'>
    					<View className='typeddh3'>{item.shareUserName}</View>
    					<View className='typeddspan'>{item.mobile}</View>
    				</View>
    				<View className='typeline'></View>
    			</View>
    			))}
	        	</View>
	        </View>
				<Footer porpNav="1"></Footer>
					</ScrollView>
      </View>
       </View>
    )
  }
}
